﻿using BusinessEntity.Xcq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Nacos.AspNetCore;
using ProjectManApi.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;


namespace ProjectManApi.Controllers.Xcq
{
    public class CzjU8 : MvcControllerBase
    {
        private readonly INacosServerManager _serverManager;
        public CzjU8(INacosServerManager serverManager) : base(serverManager)
        {
            _serverManager = serverManager;

        }

        /// <summary>
        /// 根据用户名和密码登录 反馈用户ID
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> Getuser(string username,string password)
        {
            try
            {
                DataTable user = await BusinessRule.XCQ.XcqU8.Getuser(username, password);
                if (user.Rows.Count > 0)
                {
                    return Success(user);
                }
                else
                {
                    return Success("用户名或密码错误,请重新登录！");
                }

            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        /// <summary>
        /// 根据用户ID,查询单位
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetDW(string userid)
        {
            try
            {
                DataTable user = await BusinessRule.XCQ.XcqU8.Getdw(userid);
                return Success(user);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        /// <summary>
        /// 根据一体化中的公司代码和日期 查询支付数据
        /// </summary>
        /// <param name="gsdm">公司代码,一体化中的公司代码</param>
        /// <param name="startime">开始日期</param>
        /// <param name="endtime">结束日期</param>
        /// <param name="zt">状态</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> SelecTZfInfo(string gsdm, DateTime startime, DateTime endtime, string zt)
        {
            try
            {
                DataTable user = await BusinessRule.XCQ.XcqU8.SelecTZfInfo(gsdm,startime,endtime,zt);
                return Success(user);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        
        /// <summary>
        ///  取消U8生成凭证的标志
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> UpdatePZ1(dynamic spjson)
        {
            try
            {
                JArray ja = JArray.Parse(Convert.ToString(spjson));
                int c = 0;
                foreach (JObject jb in ja)
                {
                    string PAY_CERT_ID = jb["PAY_CERT_ID"].ToString();
                    int count = await BusinessRule.XCQ.XcqU8.UpdatePZ1(PAY_CERT_ID);
                    if (count > 0)
                    {
                        int counts = await BusinessRule.XCQ.XcqU8.deleteTZfInfo(PAY_CERT_ID);
                        if (counts > 0)
                        {
                            c++;
                        }
                    }


                }
                return Success("取消凭证传入"+ja.Count+"条，共执行成功"+c+"条");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        ///  传递凭证
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> Insertpz(dynamic spjson)
        {
            try
            {
                JArray ja = JArray.Parse(Convert.ToString(spjson));
                int c = 0;
                foreach (JObject jb in ja)
                {
                    string gsdm = jb["gsdm"].ToString();//公司代码U8
                  //  string pdqj = jb["pdqj"].ToString();//当前日期yyyyMMdd
                    string kjnd = jb["kjnd"].ToString();//会计年度
                    string pdh_1 = jb["pdh"].ToString();//支付凭证编码
                    string pdh_2 = pdh_1.Substring(0,4);
                    string pdh_3 = pdh_1.Substring(5, 8);
                    string pdh = pdh_2 + pdh_3;
                    string pdqj1 =Convert.ToString(Convert.ToDateTime(jb["pdqj1"]).ToString("yyyyMMdd"));//支付日期
                    string qr_rq =Convert.ToString(Convert.ToDateTime(jb["qr_rq"]).ToString("yyyyMM"));//支付/清算日期
                    string xmdm = jb["xmdm"].ToString();//项目代码
                    string xmmc = jb["xmmc"].ToString();//项目名称
                    string czyID = jb["czyID"].ToString();// 用户ID
                    string czyname = jb["czyname"].ToString();//用户名称
                    string PAY_CERT_ID = jb["PAY_CERT_ID"].ToString();//支付凭证ID
                    string EXP_FUNC_CODE = jb["EXP_FUNC_CODE"].ToString();//功能科目
                    string PAY_AMT = jb["PAY_AMT"].ToString();//支付金额
                    string DEP_BGT_ECO_CODE = jb["DEP_BGT_ECO_CODE"].ToString();//经济科目代码
                    string USE_DES = jb["USE_DES"].ToString();//备注
                    string AGENCY_STR_CODE = jb["AGENCY_STR_CODE"].ToString();//单位支出结构编码
                    string FUND_TYPE_CODE = jb["FUND_TYPE_CODE"].ToString();//资金性质编码
                    string PAYEE_ACCT_NAME = jb["PAYEE_ACCT_NAME"].ToString();//收款人名称
                    int S = await BusinessRule.XCQ.XcqU8.Insertpz( gsdm,DateTime.Now.ToString("yyyyMMdd"),kjnd,pdh,pdqj1,qr_rq,xmdm,xmmc,czyID,czyname,PAY_CERT_ID,EXP_FUNC_CODE, PAY_AMT, DEP_BGT_ECO_CODE,USE_DES,
             AGENCY_STR_CODE,  FUND_TYPE_CODE,  PAYEE_ACCT_NAME);
                    if (S > 0)
                    {
                        int D = await BusinessRule.XCQ.XcqU8.insertPAYCERTID(PAY_CERT_ID);
                        if (D > 0)
                        {
                            c++;
                        }

                    }

                }
                return Success("凭证传入" + ja.Count + "条，共执行成功" + c + "条");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

    }
}


























































































